package gdonly.top.signin.business;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import gdonly.top.signin.R;
import gdonly.top.signin.entity.User;
import gdonly.top.signin.utils.DatabaseQuery;

/**
 * Created by zhuzhuxia on 2017/4/27.
 */
public class LoginActivity extends AppCompatActivity {
    private EditText idText;
    private EditText pwdText;
    private Button loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
    }
    private void initViews(){

        idText = (EditText) findViewById(R.id.user_id);
        pwdText = (EditText) findViewById(R.id.user_pwd);
        loginBtn = (Button) findViewById(R.id.login);
        loginBtn.setOnClickListener(listener);
    }
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.login:
                    //submit id and pwd to server and receive a json data which include info of user;
                    //json to user
                    User user = DatabaseQuery.login(idText.getText().toString().trim(), pwdText.getText().toString().trim());


                    if(user == null){
                        Toast.makeText(LoginActivity.this, "登录失败...", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Intent intent;
                    if(user.isMonitor()) {
                        intent = new Intent(LoginActivity.this, SignByMonitorActivity.class);
                    }
                    else {
                        intent = new Intent(LoginActivity.this, SignByStuActivity.class);
                    }
                    //将user存储到application中

                    ((Application)getApplication()).setThisUser(user);
                    startActivity(intent);

                    finish();
                    break;
            }
        }
    };
}
